package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.UserService;
import com.mall.user.annotation.Anonymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.gateway.controller.user
 * @date 2020/12/19 15:12
 * @Copyright © 暂时还没有
 * @description
 */
@Slf4j
@RestController
public class UserController {

    @Reference(check = false)
    UserService userService;

    @Reference(check = false)
    IKaptchaService iKaptchaService;


    @Anonymous
    @PostMapping("user/login")
    public ResponseData userLogin(@RequestBody Map<String,String> map, HttpServletRequest request, HttpServletResponse response){
        /*
          返回用户信息，包含token
         */

        //获取request信息
        String userName = map.get("userName");
        String password = map.get("userPwd");
        String captcha = map.get("captcha");//用户输入的验证码


        // 验证验证码
        String uuid = CookieUtil.getCookieValue(request,"kaptcha_uuid");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(uuid);

        log.info("----用户登录：获得uuid {}----",uuid);
        //给方法一个uuid值--------------------------------------------------
        //kaptchaCodeRequest.setUuid("2ab3d81d-46c6-42fb-ad93-4f61bdf51067");
        //----------------------------------------------------------------
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        log.info("获得kaptcha_uuid:{}" , uuid);

        //伪装验证码验证成功-------------------------------------------------
        //kaptchaCodeResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        //kaptchaCodeResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        //----------------------------------------------------------------


        if (!SysRetCodeConstants.SUCCESS.getCode().equals(kaptchaCodeResponse.getCode())){
            return new ResponseUtil().setErrorMsg("验证码错误");
        }

        //验证用户，密码,封装数据
        UserLoginRequest loginRequest = new UserLoginRequest();
        loginRequest.setUserName(userName);
        loginRequest.setPassword(password);
        UserLoginResponse loginResponse = userService.userLogin(loginRequest);

        if (loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){

            //生成cookie,写入响应体
            Cookie cookie=CookieUtil.genCookie("access_token",loginResponse.getToken(),"/",60*60);
            //CookieUtil.genCookieWithDomain("access_token",loginResponse.getToken(),60,"");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);

            //封装正常返回bean
            ResponseData responseData = new ResponseData();
            responseData.setResult(loginResponse);
            responseData.setCode(200);
            responseData.setMessage("success");
            responseData.setSuccess(true);
            return responseData;
        }

        return new ResponseUtil().setErrorMsg("用户名或密码不正确");
    }

    /*
    验证token信息是否正确
     */
    @Anonymous
    @GetMapping("user/login")
    public ResponseData userLogin(HttpServletRequest request){

        UserTokenBean userToken = null;
        ResponseData responseData = new ResponseData();

        Cookie[] cookies = request.getCookies();
        //无token
        if (cookies == null){
            responseData.setCode(500);
            responseData.setMessage("token已失效");
            responseData.setSuccess(false);
            return responseData;
        }

        for (Cookie cookie:cookies) {
            if (("access_token").equals(cookie.getName())){
                userToken = userService.userLogin(cookie.getValue());

            }
        }
        if (userToken != null){
            responseData.setResult(userToken);
            responseData.setSuccess(true);
            responseData.setCode(200);
            responseData.setMessage("success");
            return responseData;
        }

       //token解析有错误
        responseData.setCode(500);
        responseData.setMessage("token解析异常");
        responseData.setSuccess(false);
        return responseData;
    }

}
